<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class Developer extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		$this->load->library("soap");
		$this->load->library('session');
		$this->load->helper('url');
		$this->load->library('pagination');
		$this->load->helper(array('form', 'url'));
		$this->load->helper('recaptcha_helper');
		$this->load->helper("text");
		@session_start();
	}
	function check_permission() {
		if($this->session->userdata('usertype') == 1){
			return true;
		}
		else {
			return false;
		}
	}
	function manage_apps() {
		if($this->session->userdata('upname') == null){
			redirect('user/login');
		}else{
			if ($this->check_permission()) {

			}
			else {
				echo '404'; die();
			}
			$this->load->model('catetype_model');
			$this->load->model('content_model');
			$data['alltype'] = $this->catetype_model->_showalltype();
			$data['allcate'] = $this->catetype_model->showllcate();

			$this->load->model('content_model');
			$data['list_app'] = $this->content_model->get_by_user_id($this->session->userdata('upid'));
			$this->load->view('developer/dashboard',$data);
		}
	}
	function edit_app($id) {
		if($this->session->userdata('upname') == null){
			redirect('user/login');
		}else{
			if ($this->check_permission()) {
				// 				echo '404'; die();

			}
			else {
				echo '404'; die();
			}

			$this->load->model('content_model');
			$app = $this->content_model->show_detailscontent($id);
			$app = $app[0];

			if ($app->step < 4) {
				redirect('developer/add_app_step_'.($app->step+1).'/'.$app->id);
			}
			else {
				// 				$data['app'] = $app;
				// 				$this->load->model('catetype_model');
				// 				$data['alltype'] = $this->catetype_model->_showalltype();
				// 				$this->load->view('developer/dashboard',$data);
				redirect('developer/update_version/'.$app->id);
			}
		}
	}
	function manage_profile() {
		if($this->session->userdata('upname') == null) {
			redirect('user/login');
		}
		else {
			if ($this->check_permission()) {

			}
			else {
				echo '404'; die();
			}
			$this->load->view('developer/dashboard');
		}
	}
	function sales_report() {
		if($this->session->userdata('upname') == null) {
			redirect('user/login');
		}
		else {
			if ($this->check_permission()) {

			}
			else {
				echo '404'; die();
			}
			$this->load->view('developer/dashboard');
		}
	}
	function feedback() {
		if($this->session->userdata('upname') == null) {
			redirect('user/login');
		}
		else {
			if ($this->check_permission()) {

			}
			else {
				echo '404'; die();
			}
			$this->load->view('developer/dashboard');
		}
	}
	function add_app_step_1($id = null) {

		if($this->session->userdata('upname') == null) {
			redirect('user/login');
		}
		else {
			if ($this->check_permission()) {

			}
			else {
				echo '404'; die();
			}
			if (isset($_REQUEST['step-1'])) {
				$name_content = $this->input->post('namecontent');
				$data = array('namecontent'=>$name_content,'step'=>1,'userid' =>$this->session->userdata('upid'),'needhave' => 0, 'downlog'=>0,'datecreated'=>date("d-m-Y H:i:s"));
				$this->load->model('content_model');
				$id=$this->content_model->add_app($data);
				redirect('developer/add_app_step_2/'.$id);
			}

			$this->load->view('developer/dashboard');
		}
	}
	function add_app_step_2($id = null) {

		if($this->session->userdata('upname') == null) {
			redirect('user/login');
		}
		else {
			if ($this->check_permission()) {
				if ($id == null) {
					echo '404'; die();
				}
			}
			else {
				echo '404'; die();
			}
			if (isset($_REQUEST['step-2'])) {
				$active = $this->input->post('active');
				$data = array('step'=>2,'active'=>$active,'datecreated'=>date("d-m-Y H:i:s"));
				$this->load->model('content_model');
				$this->content_model->add_app($data,$id);
				redirect('developer/add_app_step_3/'.$id);
			}
			$data['id'] = $id;
			$this->load->view('developer/dashboard',$data);
		}
	}
	function add_app_step_3($id = null) {

		if($this->session->userdata('upname') == null) {
			redirect('user/login');
		}
		else {
			if ($this->check_permission()) {
				if ($id == null) {
					echo '404'; die();
				}
			}
			else {
				echo '404'; die();
			}
			if (isset($_REQUEST['step-3'])) {
				$mypath= "./src/imagescontent";
				if ( !$image=  $this->do_upload_image($mypath,'images')){
					$error = array( 'error' => $this->upload->display_errors() );
					var_dump($error);die();
				}
				$ifiles2=  $this->do_upload_image($mypath,'ifiles_2');
				$ifiles3= $this->do_upload_image($mypath,'ifiles_3');
				$mypathfile= "./apps";

				if ( ! $ifiles=  $this->do_upload_file($mypathfile,'ifiles') ) {
					$error = array( 'error' => $this->upload->display_errors() );
					var_dump($error);exit();

				}
				$version_name = $this->input->post('version-name');
				$data = array('step'=>3,
						'images'=>$image,
						'ifiles'=>$ifiles,
						'ifiles_2'=>$ifiles2,
						'ifiles_3'=>$ifiles3,
						'datecreated'=>date("d-m-Y H:i:s"));

				$this->load->model('content_model');
				$this->content_model->add_app($data,$id);
				$this->content_model->update_version($id,$ifiles,'',$version_name);
				redirect('developer/add_app_step_4/'.$id);
			}
			$data['id'] = $id;
			$this->load->view('developer/dashboard',$data);
		}
	}
	function add_app_step_4($id = null) {

		if($this->session->userdata('upname') == null) {
			redirect('user/login');
		}
		else {
			if ($this->check_permission()) {
				if ($id == null) {
					echo '404'; die();
				}
			}
			else {
				echo '404'; die();
			}
			if (isset($_REQUEST['step-4'])) {
				$typeid = $this->input->post('typeid', true);
				$description = $this->input->post('description', true);
				$cateid = $this->input->post('cateid', true);

				$data = array('step'=>4,
						'cateid'=>$cateid,
						'typeid'=>$typeid,
						'description'=>$description,
						'datecreated'=>date("d-m-Y H:i:s"));
				$this->load->model('content_model');
				$this->content_model->add_app($data,$id);
				redirect('developer/add_app_step_5/'.$id);
			}
			$data['id'] = $id;
			$this->load->model('catetype_model');
			$data['alltype'] = $this->catetype_model->_showalltype();
			$this->load->view('developer/dashboard',$data);
		}
	}
	function add_app_step_5($id = null) {

		if($this->session->userdata('upname') == null) {
			redirect('user/login');
		}
		else {
			if ($this->check_permission()) {
				if ($id == null) {
					echo '404'; die();
				}
			}
			else {
				echo '404'; die();
			}
			$this->load->view('developer/dashboard');
		}
	}
	function add_app_step_6() {
		if($this->session->userdata('upname') == null) {
			redirect('user/login');
		}
		else {
			if ($this->check_permission()) {

			}
			else {
				echo '404'; die();
			}
			$this->load->view('developer/dashboard');
		}
	}
	function edit_app_step_1() {

		if($this->session->userdata('upname') == null) {
			redirect('user/login');
		}
		else {
			if ($this->check_permission()) {

			}
			else {
				echo '404'; die();
			}
			$this->load->view('developer/dashboard');
		}
	}

	function edit_app_step_2() {

		if($this->session->userdata('upname') == null) {
			redirect('user/login');
		}
		else {
			if ($this->check_permission()) {

			}
			else {
				echo '404'; die();
			}
			$this->load->view('developer/dashboard');
		}
	}
	function edit_app_step_3() {

		if($this->session->userdata('upname') == null) {
			redirect('user/login');
		}
		else {
			if ($this->check_permission()) {

			}
			else {
				echo '404'; die();
			}
			$this->load->view('developer/dashboard');
		}
	}
	function edit_app_step_4() {

		if($this->session->userdata('upname') == null) {
			redirect('user/login');
		}
		else {
			if ($this->check_permission()) {

			}
			else {
				echo '404'; die();
			}
			$this->load->view('developer/dashboard');
		}
	}
	function edit_app_step_5() {

		if($this->session->userdata('upname') == null) {
			redirect('user/login');
		}
		else {
			if ($this->check_permission()) {

			}
			else {
				echo '404'; die();
			}
			$this->load->view('developer/dashboard');
		}
	}
	function update_version ($id) {
		if($this->session->userdata('upname') == null) {
			redirect('user/login');
		}
		else {
			if ($this->check_permission()) {
				if ($id == null) {
					echo '404'; die();
				}
			}
			else {
				echo '404'; die();
			}
			$this->load->view('developer/dashboard');
		}
	}
	function do_upload_image($mypath,$filename){
		$config['upload_path'] = $mypath;
		$config['allowed_types'] = 'gif|jpg|png|bmp';
		$config['max_size']	= '80000';
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if(isset($filename))
		{
			//echo $filename; die();
			if (!$this->upload->do_upload($filename))
			{
				$error = array('error' => $this->upload->display_errors());
				return NULL;
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$imagename = $this->upload->file_name;
				return $imagename;
			}
		}else {
			echo $this->upload->display_errors();
		}
	}

	function do_upload_file($mypath,$filename){
		$config['upload_path'] = $mypath;
		$config['allowed_types'] = 'jar|jad|apk|ipa';
		$config['max_size']	= '500000';
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if(isset($filename))
		{
			//echo $filename; die();

			if (!$this->upload->do_upload($filename))
			{
				$error = array('error' => $this->upload->display_errors());
				//print_r($error);
				return null;
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$imagename = $this->upload->file_name;
				return $imagename;
			}
		}else {
			echo $this->upload->display_errors();
		}
	}

}